import Vue from "vue";
import VueRouter from "vue-router";
import HomePage from "../views/homepage/HomePage.vue";
import Sortpage from "../views/sortpage/Sort.vue";
import MessagePage from "../views/messagepage/MessagePage.vue";
import AboutUs from "../components/aboutus/AboutUs.vue";
import CollocaItem from "../views/collocaitem/CollocaItem.vue";
import Feedback from "../components/feedback/Feedback.vue";
import VCollocationItem from "../views/collocationitem/VCollocationItem.vue";
import ShowFood from "../views/showfood/ShowFood.vue";
import RankingPage from "../views/rankingpage/RankingPage.vue";
import MyPage from "../views/mypage/MyPage.vue";
import RegisterPage from "../views/registerpage/RegisterPage.vue";
import LoginPage from "../views/loginpage/LoginPage.vue";
import ChineseFood from "../components/allfood/ZhongGuo.vue";
import TianPing from "../components/allfood/TianPing.vue";
import TraditionFood from "../components/allfood/TraditionFood.vue";
import ForeignFood from "../components/allfood/ForeignFood.vue";
import RankingChinese from "../components/rankinglist/RankingChinese.vue";
import RankingTradition from "../components/rankinglist/RankingTradition.vue";
import RankingDesserts from "../components/rankinglist/RankingDesserts.vue";
import RankingAmerican from "../components/rankinglist/RankingAmerican.vue";
import MyHistory from "../components/mymesssage/MyHistory.vue";
import SearchResult from "../components/searchresult/SearchResult.vue";
import Administration from "../views/administration/Administration.vue";
import AdministrationMessage from "../views/administration/AdministrationMessage.vue";
import AdminFood from "../views/administration/AdminFood.vue";
import InsertFoods from "../views/administration/InsertFoods.vue";
import AdminCollocation from "../views/administration/AdminCollocation.vue";
import InsertAdminColl from "../views/administration/InsertAdminColl.vue";
import Forget from "../views/forget/Forget.vue";
import UpdateFoods from "../views/administration/UpdateFoods.vue";
import Admin from "../components/administrations/Admin.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "首页",
    component: HomePage,
  },
  {
    path: "/homepage",
    name: "首页",
    component: HomePage,
  },
  {
    path: "/sortpage",
    name: "Sortpage",
    component: Sortpage,
  },
  {
    path: "/rankingpage",
    name: "RankingPage",
    component: RankingPage,
  },
  {
    path: "/chinesefood",
    name: "ChineseFood",
    component: ChineseFood,
  },
  {
    path: "/traditionfood",
    name: "TraditionFood",
    component: TraditionFood,
  },
  {
    path: "/foreignfood",
    name: "ForeignFood",
    component: ForeignFood,
  },
  {
    path: "/tianping",
    name: "TianPing",
    component: TianPing,
  },
  {
    path: "/messagepage",
    name: "MessagePage",
    component: MessagePage,
  },
  {
    path: "/myhistory",
    name: "MyHistory",
    meta: {
      requireAuth: true,
    },
    component: MyHistory,
  },
  {
    path: "/aboutus",
    name: "AboutUs",
    component: AboutUs,
  },
  {
    path: "/feedback",
    name: "Feedback",
    component: Feedback,
  },
  {
    path: "/collocaitem",
    name: "CollocaItem",
    component: CollocaItem,
  },
  {
    path: "/vcollocationitem",
    name: "VCollocationItem",
    component: VCollocationItem,
  },
  {
    path: "/rankingchinese",
    name: "RankingChinese",
    component: RankingChinese,
  },
  {
    path: "/rankingdesserts",
    name: "RankingDesserts",
    component: RankingDesserts,
  },
  {
    path: "/rankingamerican",
    name: "RankingAmerican",
    component: RankingAmerican,
  },
  {
    path: "/rankingtradition",
    name: "RankingTradition",
    component: RankingTradition,
  },
  {
    path: "/showfood",
    name: "ShowFood",
    component: ShowFood,
  },

  {
    path: "/mypage",
    name: "MyPage",
    meta: {
      requireAuth: true,
    },
    component: MyPage,
  },
  {
    path: "/registerpage",
    name: "RegisterPage",
    component: RegisterPage,
  },
  {
    path: "/loginpage",
    name: "LoginPage",
    component: LoginPage,
  },
  {
    path: "/searchresult",
    name: "SearchResult",
    component: SearchResult,
  },

  {
    path: "/admin",
    children: [
      {
        path: "",
        redirect: "administration",
      },

      {
        path: "administration",
        name: "Administration",
        meta: {
          requireAuths: true,
        },
        component: Administration,
      },

      {
        path: "administrationmessage",
        name: "AdministrationMessage",
        meta: {
          requireAuths: true,
        },
        component: AdministrationMessage,
      },

      {
        path: "adminfood",
        name: "AdminFood",
        meta: {
          requireAuths: true,
        },
        component: AdminFood,
      },

      {
        path: "insertfoods",
        name: "InsertFoods",
        meta: {
          requireAuths: true,
        },
        component: InsertFoods,
      },

      {
        path: "admincollocation",
        name: "AdminCollocation",
        meta: {
          requireAuths: true,
        },
        component: AdminCollocation,
      },

      {
        path: "insertadmincoll",
        name: "InsertAdminColl",
        meta: {
          requireAuths: true,
        },
        component: InsertAdminColl,
      },

      {
        path: "updatefoods",
        name: "UpdateFoods",
        meta: {
          requireAuths: true,
        },
        component: UpdateFoods,
      },
    ],
    meta: {
      requireAuths: true,
    },
    component: Admin,
  },

  {
    path: "/forget",
    name: "Forget",
    component: Forget,
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  document.title = to.name;
  next();
});

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    // 判断该路由是否需要登录权限
    if (
      sessionStorage.getItem("userid") != "null" &&
      sessionStorage.getItem("userid") != null
    ) {
      // 判断本地是否存在token
      next();
    } else {
      // 未登录,跳转到登陆页面
      next({
        path: "/loginpage",
      });
    }
  } else {
    next();
  }
  // sessionStorage.getItem('usertype')
  if (to.meta.requireAuths) {
    if (
      sessionStorage.getItem("userid") != "null" &&
      sessionStorage.getItem("userid") != null &&
      sessionStorage.getItem("usertype") != "null" &&
      sessionStorage.getItem("usertype") != null
    ) {
      next();
    } else {
      next({
        path: "/loginpage",
      });
    }
  } else {
    next();
  }
});

export default router;
